package com.example.schoolnewsapp;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.schoolnewsapp.JavaBean.news_info;


import com.example.schoolnewsapp.adapter.NewsSingleAdapter;
import com.example.schoolnewsapp.databinding.FragmentMainBinding;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private FragmentMainBinding binding;
    private List<news_info> news = new ArrayList<>();

    private boolean isRefresh = false;
    private int position = -1;


    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentMainBinding.inflate(inflater,container,false);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        MediaNewsData();
        MediaSwipe();
        binding.mainTotal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_1:
                        binding.viewPager.removeAllViews();
                        MediaNewsData();

                }
            }
        });
        return binding.getRoot();
    }
    private void MediainitView() {
        NewsSingleAdapter adapter;

        binding.viewPager.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        binding.viewPager.setItemAnimator(new DefaultItemAnimator());
        adapter = new NewsSingleAdapter(news, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final NewsSingleAdapter.ViewHolder viewHolder = (NewsSingleAdapter.ViewHolder) v.getTag();
                position = viewHolder.getAdapterPosition();
                news_info mediafocus = news.get(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("news_info", (Serializable) mediafocus);
                intent.putExtras(bundle);
                intent.setClass(getContext(), SingleNewsActivity.class);
                startActivity(intent);
            }
        });
        binding.viewPager.setAdapter(adapter);
    }


    private void MediaNewsData() {
        //此处替换为你的用户名密码
        BmobQuery<news_info> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<news_info>() {
            @Override
            public void done(List<news_info> list, BmobException e) {
                if (e == null) {
                    news = list;
                    System.out.println("获取成功");
                    MediainitView();
                } else {
                    System.out.println(e.getMessage());
                }
            }
        });

//        String sql="select * from news_info  limit 0,20";
//        new BmobQuery<news_info>().doSQLQuery(sql, new SQLQueryListener<news_info>() {
//            @Override
//            public void done(BmobQueryResult<news_info> bmobQueryResult, BmobException e) {
//                if (e==null)
//                {
//                    List<news_info> list=(List<news_info>) bmobQueryResult.getResults();
//                    if (list!=null&&list.size()>0)
//                    {
//                        mediaformats.addAll(list);
//                        MediainitView();
//                    }
//                    else{
//                        Log.i("TAG","查询成功,无数据返回");
//                    }
//                }
//                else
//                {
//                    Log.i("TAG","查询失败,错误码:"+e.getErrorCode()+"错误描述:"+e.getMessage());
//                }
//            }
//
//        });
   }

    private void MediaSwipe(){
        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MediainitView();
                        binding.swipeRefreshLayout.setRefreshing(false);

                    }
                },2000);
            }
        });
    }

    @Override
    public void onRefresh() {
        if (!isRefresh) {
            isRefresh = true;
            binding.swipeRefreshLayout.setRefreshing(false);
            isRefresh = false;
        }
    }
}